package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/dish")
public class DishController {
    private final DishService dishService;

    //菜品分页查询
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        return Result.success(dishService.pageQuery(dishPageQueryDTO));
    }

    //菜品起售、停售
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public Result<Dish> updateStatus(@PathVariable Integer status,String id) {
        log.info("菜品起售、停售：{},id：{}",status,id);
        dishService.updateStatus(status,id);
        return Result.success();
    }

    //根据分类id查询菜品
    @GetMapping("/list")
    public Result<List<Dish>> selectByCategoryId(Integer categoryId) {
        log.info("根据分类id查询菜品:{}",categoryId);
        return Result.success(dishService.selectByCategoryId(categoryId));
    }

    //新增菜品
    @PostMapping()
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public Result addDishWithFlavor(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}",dishDTO);
        dishService.addDishWithFlavor(dishDTO);
        return Result.success();
    }

    //根据id查询菜品
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Long id) {
        log.info("根据id查询菜品:{}",id);
        return Result.success(dishService.selectById(id));
    }

    //批量删除菜品
    @DeleteMapping()
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品:{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    //修改菜品
    @PutMapping()
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}",dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }
}

